import { createRouter, createWebHistory } from 'vue-router'
import Home from '@/views/home.vue'
import My from '@/views/my.vue'
import Login from '@/views/login.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/home',
      name: 'home',
      component: Home,
      children:[{
        path:'my',
        name:'my',
        component:My
      }]
    },
    {
      path:'/login',
      name:'login',
      component:Login
    }
  
  ],
})
router.beforeEach((to,from,next)=>{
  let token=localStorage.getItem('token')
  if(to.fullPath=='/home/my'){
    if(!token){
      next('/login')
    }
    else{
      next()
    }

  }
  else{
    next()
  }
})

export default router
